/************************************************************************
 * <br>
 * <ul>
 * <li>版权所有：唐山弘川科技有限公司 2009－2020</li>
 * <li>创建日期：2009-8-10</li>
 * <li>初始作者：刘川</li>
 * </ul>
 * <ul>
 * <b>修订历史</b>
 * <li>1、</li>
 * </ul>
 ***********************************************************************/
package com.hy.enterprise.framework.persistence.domain.query ;

import static com.hy.enterprise.framework.persistence.domain.query.internal.managedobjects.SimpleAggregateExpression.Function.AVG;
import static com.hy.enterprise.framework.persistence.domain.query.internal.managedobjects.SimpleAggregateExpression.Function.COUNT;
import static com.hy.enterprise.framework.persistence.domain.query.internal.managedobjects.SimpleAggregateExpression.Function.MAX;
import static com.hy.enterprise.framework.persistence.domain.query.internal.managedobjects.SimpleAggregateExpression.Function.MIN;
import static com.hy.enterprise.framework.persistence.domain.query.internal.managedobjects.SimpleAggregateExpression.Function.SUM;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

import com.hy.enterprise.framework.persistence.domain.query.internal.AliasFactory;
import com.hy.enterprise.framework.persistence.domain.query.internal.LibraPersistentObjectFactory;
import com.hy.enterprise.framework.persistence.domain.query.internal.managedobjects.DistinctExpression;
import com.hy.enterprise.framework.persistence.domain.query.internal.managedobjects.LibraManagedObject;
import com.hy.enterprise.framework.persistence.domain.query.internal.managedobjects.SimpleAggregateExpression;

/**
 * <ul>
 * <li>设计作者：刘川</li>
 * <li>设计日期：2009-8-10</li>
 * <li>设计时间：下午03:20:23</li>
 * <li>设计目的：聚合操作实现类</li>
 * </ul>
 * <ul>
 * <b>修订历史</b>
 * <li>1、</li>
 * </ul>
 */
public final class Aggregates
{
	private static final LibraPersistentObjectFactory	factory	=
	                                                              LibraPersistentObjectFactory.INSTANCE ;

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-10</li>
	 * <li>设计时间：下午03:25:27</li>
	 * <li>设计目的：计算平均值</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param <N>
	 * @param what
	 * @return
	 */
	public static < N extends Number > Double avg (
	                                                N what )
	{
		Double ret =
		             Aggregates.factory
		                               .makeNew ( Double.class ) ;
		LibraManagedObject libraManagedObjectAboutArg =
		                                                AliasFactory
		                                                            .about ( what ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        AVG ,
		                                                        libraManagedObjectAboutArg ) ) ;
		return ret ;

	}

	public static Long count (
	                           Object object )
	{
		Long ret =
		           Aggregates.factory
		                             .makeNew ( Long.class ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( object ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        COUNT ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	public static < T > T distinct (
	                                 T what )
	{
		T ret =
		        Aggregates.factory
		                          .makeNew ( what ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( what ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new DistinctExpression (
		                                                 aboutArg ) ) ;
		return ret ;
	}

	public static Character max (
	                              Character what )
	{
		Character ret =
		                Aggregates.factory
		                                  .makeNew ( Character.class ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( what ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        MAX ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	@ SuppressWarnings ( "unchecked" )
	public static < D extends Date > D max (
	                                         D what )
	{
		D ret =
		        ( D ) Aggregates.factory
		                                .makeNew ( what
		                                               .getClass ( ) ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( what ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        MAX ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	@ SuppressWarnings ( "unchecked" )
	public static < N extends Number > N max (
	                                           N what )
	{
		N ret =
		        ( N ) Aggregates.factory
		                                .makeNew ( what
		                                               .getClass ( ) ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( what ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        MAX ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	public static String max (
	                           String what )
	{
		String ret =
		             Aggregates.factory
		                               .makeNew ( String.class ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( what ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        MAX ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	public static Character min (
	                              Character what )
	{
		Character ret =
		                Aggregates.factory
		                                  .makeNew ( Character.class ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( what ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        MIN ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	@ SuppressWarnings ( "unchecked" )
	public static < D extends Date > D min (
	                                         D what )
	{
		D ret =
		        ( D ) Aggregates.factory
		                                .makeNew ( what
		                                               .getClass ( ) ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( what ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        MIN ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	// See spec 4.8.4
	@ SuppressWarnings ( "unchecked" )
	public static < N extends Number > N min (
	                                           N what )
	{
		N ret =
		        ( N ) Aggregates.factory
		                                .makeNew ( what
		                                               .getClass ( ) ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( what ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        MIN ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	public static String min (
	                           String what )
	{
		String ret =
		             Aggregates.factory
		                               .makeNew ( String.class ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( what ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        MIN ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	public static Object star ( )
	{
		Long ret =
		           Aggregates.factory
		                             .makeNew ( Long.class ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new LibraManagedObject ( )
		                        {
			                        @ Override
			                        public String getTargetRepresentation ( )
			                        {
				                        return "*" ;
			                        }
		                        } ) ;
		return ret ;
	}

	public static BigDecimal sum (
	                               BigDecimal n )
	{
		BigDecimal ret =
		                 Aggregates.factory
		                                   .makeNew ( BigDecimal.class ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( n ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        SUM ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	public static BigInteger sum (
	                               BigInteger n )
	{
		BigInteger ret =
		                 Aggregates.factory
		                                   .makeNew ( BigInteger.class ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( n ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        SUM ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	public static Double sum (
	                           Double n )
	{
		Double ret =
		             Aggregates.factory
		                               .makeNew ( Double.class ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( n ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        SUM ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	public static Double sum (
	                           Float n )
	{
		Double ret =
		             Aggregates.factory
		                               .makeNew ( Double.class ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( n ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        SUM ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	public static Long sum (
	                         Number n )
	{
		Long ret =
		           Aggregates.factory
		                             .makeNew ( Long.class ) ;
		LibraManagedObject aboutArg =
		                              AliasFactory
		                                          .about ( n ) ;
		AliasFactory
		            .remember (
		                        ret ,
		                        new SimpleAggregateExpression (
		                                                        SUM ,
		                                                        aboutArg ) ) ;
		return ret ;
	}

	/**
	 * 构造函数
	 */
	private Aggregates ( )
	{
		super ( ) ;
	}
}
